import socket

# 本地地址和端口号
server_address = ('localhost', 1234)

# 创建TCP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定地址和端口号
server_socket.bind(server_address)

# 监听连接
server_socket.listen(1)
print("Waiting for incoming connections...")

# 接受连接
client_socket, client_address = server_socket.accept()
print("Accepted connection from", client_address)

# 接收客户端消息
data = client_socket.recv(1024)
print("Received message:", data.decode())

# 回复客户端
response = "Hello, client!"
client_socket.send(response.encode())

# 关闭连接
client_socket.close()
server_socket.close()
